package com.ficus.road.maintain.util;

import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.config.BaiduMapConfig;
import com.ficus.road.maintain.core.model.GpsLocation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Component
@Slf4j
public class BaiduMapClient {

    @Autowired
    private BaiduMapConfig baiduMapConfig;

    @Autowired
    private RestTemplate restTemplate;

    public static void main(String[] args) {
        GpsLocation gpsLocation = GpsLocation.builder().longitude(121.602276722).latitude(31.241192991).build();
        BaiduMapClient baiduMapClient = new BaiduMapClient();
        String geocoderUrl = "http://api.map.baidu.com" + "/reverse_geocoding/v3/";
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(geocoderUrl)
                .queryParam("ak", "RK5uIRIhFh2BTNZGA55p5Xv8KtPdzqn2")
                .queryParam("output", "json")
                .queryParam("coordtype", "wgs84ll")
                .queryParam("location", gpsLocation.getLatitude() + "," + gpsLocation.getLongitude());
        ResponseEntity<String> exchange = new RestTemplate().exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        if (exchange.getStatusCode() == HttpStatus.OK) {
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            Integer status = jsonObject.getInteger("status");
            if (status != 0) {
                String message = jsonObject.getString("message");
                log.error("根据gps查询道路信息失败,返回码：{}, 错误信息:{}", status, message);
            } else {
                JSONObject result = jsonObject.getJSONObject("result");
                if (!ObjectUtils.isEmpty(result) && !ObjectUtils.isEmpty(result.getJSONObject("addressComponent"))) {
                    System.out.println(result.getJSONObject("addressComponent").getString("street"));
                    ;
                }
            }
        }
    }

    /**
     * 跟据gps反查路名信息
     *
     * @return
     */
    public String geocoder(GpsLocation gpsLocation) {
        String geocoderUrl = this.baiduMapConfig.getEndpoint() + this.baiduMapConfig.getGeocoder();
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(geocoderUrl)
                .queryParam("ak", this.baiduMapConfig.getAccessKey())
                .queryParam("output", "json")
                .queryParam("coordtype", "wgs84ll")
                .queryParam("location", gpsLocation.getLatitude() + "," + gpsLocation.getLongitude());
        ResponseEntity<String> exchange = this.restTemplate.exchange(builder.toUriString(), HttpMethod.GET, null, String.class);
        if (exchange.getStatusCode() == HttpStatus.OK) {
            JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
            Integer status = jsonObject.getInteger("status");
            if (status != 0) {
                String message = jsonObject.getString("message");
                log.error("根据gps查询道路信息失败,返回码：{}, 错误信息:{}", status, message);
            } else {
                JSONObject result = jsonObject.getJSONObject("result");
                if (!ObjectUtils.isEmpty(result) && !ObjectUtils.isEmpty(result.getJSONObject("addressComponent"))) {
                    return result.getJSONObject("addressComponent").getString("street");
                }
            }
        }
        return null;
    }
}
